
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>29.14. fpectl --- Floating point exception control &#8212; Python 3.6.6rc1 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.6.6rc1 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="30. Custom Python Interpreters" href="custominterp.html" />
    <link rel="prev" title="29.13. site --- Site-specific configuration hook" href="site.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/fpectl.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="custominterp.html" title="30. Custom Python Interpreters"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="site.html" title="29.13. site --- Site-specific configuration hook"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">29. Python Runtime Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-fpectl">
<span id="fpectl-floating-point-exception-control"></span><h1>29.14. <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> --- Floating point exception control<a class="headerlink" href="#module-fpectl" title="永久链接至标题">¶</a></h1>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module is not built by default, and its usage is discouraged
and may be dangerous except in the hands of experts.  See also the section
<a class="reference internal" href="#fpectl-limitations"><span class="std std-ref">Limitations and other considerations</span></a> on limitations for more details.</p>
</div>
<hr class="docutils" id="index-0" />
<p>Most computers carry out floating point operations in conformance with the
so-called IEEE-754 standard. On any real computer, some floating point
operations produce results that cannot be expressed as a normal floating point
value. For example, try</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">inf</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="o">/</span> <span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">nan</span>
</pre></div>
</div>
<p>(The example above will work on many platforms. DEC Alpha may be one exception.)
&quot;Inf&quot; is a special, non-numeric value in IEEE-754 that stands for &quot;infinity&quot;,
and &quot;nan&quot; means &quot;not a number.&quot; Note that, other than the non-numeric results,
nothing special happened when you asked Python to carry out those calculations.
That is in fact the default behaviour prescribed in the IEEE-754 standard, and
if it works for you, stop reading now.</p>
<p>In some circumstances, it would be better to raise an exception and stop
processing at the point where the faulty operation was attempted. The
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module is for use in that situation. It provides control over
floating point units from several hardware manufacturers, allowing the user to
turn on the generation of <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code> whenever any of the IEEE-754
exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem
with a pair of wrapper macros that are inserted into the C code comprising your
python system, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code> is trapped and converted into the Python
<a class="reference internal" href="exceptions.html#FloatingPointError" title="FloatingPointError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FloatingPointError</span></code></a> exception.</p>
<p>The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module defines the following functions and may raise the given
exception:</p>
<dl class="function">
<dt id="fpectl.turnon_sigfpe">
<code class="descclassname">fpectl.</code><code class="descname">turnon_sigfpe</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#fpectl.turnon_sigfpe" title="永久链接至目标">¶</a></dt>
<dd><p>Turn on the generation of <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>, and set up an appropriate signal
handler.</p>
</dd></dl>

<dl class="function">
<dt id="fpectl.turnoff_sigfpe">
<code class="descclassname">fpectl.</code><code class="descname">turnoff_sigfpe</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#fpectl.turnoff_sigfpe" title="永久链接至目标">¶</a></dt>
<dd><p>Reset default handling of floating point exceptions.</p>
</dd></dl>

<dl class="exception">
<dt id="fpectl.FloatingPointError">
<em class="property">exception </em><code class="descclassname">fpectl.</code><code class="descname">FloatingPointError</code><a class="headerlink" href="#fpectl.FloatingPointError" title="永久链接至目标">¶</a></dt>
<dd><p>After <a class="reference internal" href="#fpectl.turnon_sigfpe" title="fpectl.turnon_sigfpe"><code class="xref py py-func docutils literal notranslate"><span class="pre">turnon_sigfpe()</span></code></a> has been executed, a floating point operation that
raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid
operation will in turn raise this standard Python exception.</p>
</dd></dl>

<div class="section" id="example">
<span id="fpectl-example"></span><h2>29.14.1. Example<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h2>
<p>The following example demonstrates how to start up and test operation of the
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpectl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fpetest</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpectl</span><span class="o">.</span><span class="n">turnon_sigfpe</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fpetest</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
<span class="go">overflow        PASS</span>
<span class="go">FloatingPointError: Overflow</span>

<span class="go">div by 0        PASS</span>
<span class="go">FloatingPointError: Division by zero</span>
<span class="go">  [ more output from test elided ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">FloatingPointError</span>: <span class="n">in math_1</span>
</pre></div>
</div>
</div>
<div class="section" id="limitations-and-other-considerations">
<span id="fpectl-limitations"></span><h2>29.14.2. Limitations and other considerations<a class="headerlink" href="#limitations-and-other-considerations" title="永久链接至标题">¶</a></h2>
<p>Setting up a given processor to trap IEEE-754 floating point errors currently
requires custom code on a per-architecture basis. You may have to modify
<a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> to control your particular hardware.</p>
<p>Conversion of an IEEE-754 exception to a Python exception requires that the
wrapper macros <code class="docutils literal notranslate"><span class="pre">PyFPE_START_PROTECT</span></code> and <code class="docutils literal notranslate"><span class="pre">PyFPE_END_PROTECT</span></code> be inserted
into your code in an appropriate fashion.  Python itself has been modified to
support the <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module, but many other codes of interest to numerical
analysts have not.</p>
<p>The <a class="reference internal" href="#module-fpectl" title="fpectl: Provide control for floating point exception handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fpectl</span></code></a> module is not thread-safe.</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last">Some files in the source distribution may be interesting in learning more about
how this module operates. The include file <code class="file docutils literal notranslate"><span class="pre">Include/pyfpe.h</span></code> discusses the
implementation of this module at some length. <code class="file docutils literal notranslate"><span class="pre">Modules/fpetestmodule.c</span></code>
gives several examples of use. Many additional examples can be found in
<code class="file docutils literal notranslate"><span class="pre">Objects/floatobject.c</span></code>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">內容目录</a></h3>
  <ul>
<li><a class="reference internal" href="#">29.14. <code class="docutils literal notranslate"><span class="pre">fpectl</span></code> --- Floating point exception control</a><ul>
<li><a class="reference internal" href="#example">29.14.1. Example</a></li>
<li><a class="reference internal" href="#limitations-and-other-considerations">29.14.2. Limitations and other considerations</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="site.html"
                        title="上一章">29.13. <code class="docutils literal notranslate"><span class="pre">site</span></code> --- Site-specific configuration hook</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="custominterp.html"
                        title="下一章">30. Custom Python Interpreters</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.6/Doc/library/fpectl.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="custominterp.html" title="30. Custom Python Interpreters"
             >下一页</a> |</li>
        <li class="right" >
          <a href="site.html" title="29.13. site --- Site-specific configuration hook"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="python.html" >29. Python Runtime Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on 6月 24, 2018.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.2.
    </div>

  </body>
</html>